/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageManagerDialogBrowseFileSample.java
 *
 * Created on Feb 17, 2009, 8:10:36 PM
 */
package test;

import com.utils.MapFileFilter;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 *
 * @author DMC
 */
public class ImageManagerDialogBrowseFileSample extends javax.swing.JFrame {

    /** Creates new form ImageManagerDialogBrowseFileSample */
    public ImageManagerDialogBrowseFileSample() {
        initComponents();
        MapFileFilter mff = new MapFileFilter("gif", "File GIF Only");
        MapFileFilter mff1 = new MapFileFilter("png", "File PNG Only");

        jf.setFileHidingEnabled(false);
        jf.setAcceptAllFileFilterUsed(false);
        jf.setFileFilter(mff);
        jf.setFileFilter(mff1);
        jf.setFileFilter(new MapFileFilter(true));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jf = new JFileChooser();
        jScrollPaneImagePreview = new JScrollPane();
        jPanel1 = new JPanel();
        jLabelWidth = new JLabel();
        jLabelHeight = new JLabel();
        jLabelValueWidth = new JLabel();
        jLabelValueHeight = new JLabel();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Browse Image File");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new AbsoluteLayout());

        jf.setFileHidingEnabled(true);
        jf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jfActionPerformed(evt);
            }
        });
        jf.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                jfPropertyChange(evt);
            }
        });
        getContentPane().add(jf, new AbsoluteConstraints(0, 0, 450, 440));

        jPanel1.setLayout(new GridLayout(1, 0));
        jScrollPaneImagePreview.setViewportView(jPanel1);

        getContentPane().add(jScrollPaneImagePreview, new AbsoluteConstraints(450, 0, 330, 370));

        jLabelWidth.setText("Width");
        getContentPane().add(jLabelWidth, new AbsoluteConstraints(650, 380, -1, 20));

        jLabelHeight.setText("Height");
        getContentPane().add(jLabelHeight, new AbsoluteConstraints(650, 410, -1, -1));
        getContentPane().add(jLabelValueWidth, new AbsoluteConstraints(690, 380, 90, 20));
        getContentPane().add(jLabelValueHeight, new AbsoluteConstraints(690, 410, 90, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jfActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jfActionPerformed
        String comm = evt.getActionCommand();
        if (JFileChooser.APPROVE_SELECTION.equals(comm)) {
            File file = (File) jf.getSelectedFile();
            if (file.exists() && jf.accept(file)) {
                this.dispose();
            }
        } else if (JFileChooser.CANCEL_SELECTION.equals(comm)) {
            this.dispose();
        }
    }//GEN-LAST:event_jfActionPerformed

    private void jfPropertyChange(PropertyChangeEvent evt) {//GEN-FIRST:event_jfPropertyChange
        String prop = evt.getPropertyName();
        if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop) && evt.getNewValue() != null) {
            File file = (File) evt.getNewValue();
            if (file.exists() && jf.accept(file)) {
                ImageIcon im = new ImageIcon(file.getAbsolutePath());
                Dimension dim = new Dimension(im.getIconWidth(), im.getIconHeight());
                jLabelValueWidth.setText(": " + dim.getWidth() + "px");
                jLabelValueHeight.setText(": " + dim.getHeight() + "px");
                jPanel1.removeAll();
                jPanel1.setMinimumSize(dim);
                jPanel1.add(new JLabel(im));
            }
        }
    }//GEN-LAST:event_jfPropertyChange

    private void formWindowClosing(WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JFrame.setDefaultLookAndFeelDecorated(true);
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ImageManagerDialogBrowseFileSample().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JLabel jLabelHeight;
    private JLabel jLabelValueHeight;
    private JLabel jLabelValueWidth;
    private JLabel jLabelWidth;
    private JPanel jPanel1;
    private JScrollPane jScrollPaneImagePreview;
    private JFileChooser jf;
    // End of variables declaration//GEN-END:variables
}
